package com.ruoyi.appService.mentalTestResult.careeranchorResult.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.ruoyi.appService.mentalTestResult.huolandResult.domain.ZTestHuolandTestresult;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.WxTokenService;
import com.ruoyi.framework.web.service.wxUserRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.appService.mentalTestResult.careeranchorResult.mapper.ZTestCareeranchorTestresultMapper;
import com.ruoyi.appService.mentalTestResult.careeranchorResult.domain.ZTestCareeranchorTestresult;
import com.ruoyi.appService.mentalTestResult.careeranchorResult.service.IZTestCareeranchorTestresultService;

import javax.servlet.http.HttpServletRequest;

/**
 * 职业锚心理测试结果Service业务层处理
 *
 * @author anjiao
 * @date 2025-05-15
 */
@Service
public class ZTestCareeranchorTestresultServiceImpl extends ServiceImpl<ZTestCareeranchorTestresultMapper, ZTestCareeranchorTestresult> implements IZTestCareeranchorTestresultService {
    @Autowired
    private ZTestCareeranchorTestresultMapper zTestCareeranchorTestresultMapper;
@Autowired
private WxTokenService wxTokenService;
    /**
     * 查询职业锚心理测试结果
     *
     * @param id 职业锚心理测试结果主键
     * @return 职业锚心理测试结果
     */
    @Override
    public ZTestCareeranchorTestresult selectZTestCareeranchorTestresultById(Long id) {
        return zTestCareeranchorTestresultMapper.selectZTestCareeranchorTestresultById(id);
    }

    /**
     * 查询职业锚心理测试结果列表
     *
     * @param zTestCareeranchorTestresult 职业锚心理测试结果
     * @return 职业锚心理测试结果
     */
    @Override
    public List<ZTestCareeranchorTestresult> selectZTestCareeranchorTestresultList(ZTestCareeranchorTestresult zTestCareeranchorTestresult) {
        return zTestCareeranchorTestresultMapper.selectZTestCareeranchorTestresultList(zTestCareeranchorTestresult);
    }

    /**
     * 新增职业锚心理测试结果
     *
     * @param zTestCareeranchorTestresult 职业锚心理测试结果
     * @return 结果
     */
    @Override
    public int insertZTestCareeranchorTestresult(ZTestCareeranchorTestresult zTestCareeranchorTestresult) {
            return zTestCareeranchorTestresultMapper.insertZTestCareeranchorTestresult(zTestCareeranchorTestresult);
    }

    /**
     * 修改职业锚心理测试结果
     *
     * @param zTestCareeranchorTestresult 职业锚心理测试结果
     * @return 结果
     */
    @Override
    public int updateZTestCareeranchorTestresult(ZTestCareeranchorTestresult zTestCareeranchorTestresult) {
        return zTestCareeranchorTestresultMapper.updateZTestCareeranchorTestresult(zTestCareeranchorTestresult);
    }

    /**
     * 批量删除职业锚心理测试结果
     *
     * @param ids 需要删除的职业锚心理测试结果主键
     * @return 结果
     */
    @Override
    public int deleteZTestCareeranchorTestresultByIds(Long[] ids) {
        return zTestCareeranchorTestresultMapper.deleteZTestCareeranchorTestresultByIds(ids);
    }

    /**
     * 删除职业锚心理测试结果信息
     *
     * @param id 职业锚心理测试结果主键
     * @return 结果
     */
    @Override
    public int deleteZTestCareeranchorTestresultById(Long id) {
        return zTestCareeranchorTestresultMapper.deleteZTestCareeranchorTestresultById(id);
    }

    @Override
    public void addOrupdateTestResult(ZTestCareeranchorTestresult zTestCareeranchorTestresult, HttpServletRequest request) {

//        先获取用户id
        String token = wxTokenService.getToken(request);
        wxUserRedis wxuserRedis = wxTokenService.getwxUser(token);
        int userId=wxuserRedis.getUserId();


//根据userid判断用户是否做过测试
        QueryWrapper qw1 = new QueryWrapper<>();
        qw1.eq("userid", userId);
        ZTestCareeranchorTestresult isExist  = zTestCareeranchorTestresultMapper.selectOne(qw1);
        if (StringUtils.isNotNull(isExist)) {
//            已经做过测试，执行修改逻辑

            UpdateWrapper<ZTestCareeranchorTestresult> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("userid", userId);
            zTestCareeranchorTestresultMapper.update(zTestCareeranchorTestresult, updateWrapper);

        }else{
//            第一次做测试，执行新增逻辑
            zTestCareeranchorTestresult.setUserid ((long) userId);
            zTestCareeranchorTestresultMapper.insertZTestCareeranchorTestresult(zTestCareeranchorTestresult);
        }
    }

    @Override
    public AjaxResult selectTestResult(HttpServletRequest request) {
        //        先获取用户id
        String token = wxTokenService.getToken(request);
        wxUserRedis wxuserRedis = wxTokenService.getwxUser(token);
        int userId=wxuserRedis.getUserId();

        //查询测试结果
        QueryWrapper qw1 = new QueryWrapper<>();
        qw1.eq("userid", userId);
        ZTestCareeranchorTestresult   testresult  = zTestCareeranchorTestresultMapper.selectOne(qw1);

        if(StringUtils.isNull(testresult)){
            return AjaxResult.error("该用户没有进行测试");
        }else {
            return  AjaxResult.success(testresult);
        }
    }


}